home *** CD-ROM | disk | FTP | other *** search
/ START Magazine / START VOL 4 NO 3.st / CINIT.ARC / LASINIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-12  |  2.4 KB  |  79 lines

  1. /****    init.c - Laser program/accessory startup *****/
  2.  
  3. /*                     by Samuel Streeper            */
  4. /*              Copyright 1989 Antic Publishing      */
  5.  
  6.  
  7. /* Base page definitions */
  8. #define codelen 12        /* Code segment length */
  9. #define datalen 20        /* Data segment length */
  10. #define bsslen 28        /* Bss  segment length */
  11. #define par_bp 36        /* Parent basepage */
  12.  
  13. extern _main(), main(), _dbuginit();
  14. char *_base;            /* points to base page of program */
  15. int _app;                /* 0xff=program is app, 0=program is accessory */
  16.  
  17. long _progsize;            /* since I must calc this, I will leave this */
  18.                         /* value handy for terminate-stay resident progs */
  19. int errno;
  20. extern char _stack[];
  21. extern long _stksize;
  22.  
  23. asm {
  24.     _main:
  25.         ; I know I am running as a desk accessory by 2 tests:
  26.         ; if my initial stack is zero, or if I have a null
  27.         ; pointer to my parents basepage (All PROGRAMS have a
  28.         ; parent, be it the desktop or another program.
  29.         ; Null stack is undocumented, though! (so don't count on it)
  30.         ; note 2:
  31.         ; At startup, a program may get its basepage address from the
  32.         ; stack, ie
  33.         ;
  34.         ;            move.l 4(sp),A5        ;A5 points to basepage
  35.         ;
  36.         ; For an accessory, however, the stack pointer is null and
  37.         ; this generates a bus error, so we assume that the basepage
  38.         ; immediately precedes the program, which is true if the
  39.         ; desktop or most shells executed the program. The following
  40.         ; code could be tweaked to accomodate discontiguous basepages
  41.         ; if necessary.
  42.  
  43.         lea        _main, A5        ;Compute basepage address
  44.         suba    #0x100, A5        ;subtract size of basepage
  45.         lea        _stack,A7
  46.         adda.l    _stksize,A7        ;set up our stack
  47.  
  48.         moveq    #0,D5
  49.         tst.l   par_bp(A5)        ;Parent basepage pointer clear if ACC
  50.         sne        D5                ;D5 set if application
  51.         move    D5,_app
  52.         beq     cont            ;is an accessory
  53.  
  54.     app:
  55.             ; if we are here, our thing is being run as a program, not
  56.             ; an accessory.
  57.  
  58.         move.l    codelen(A5),D0
  59.         add.l    datalen(A5),D0
  60.         add.l    bsslen(A5),D0
  61.         addi.l    #0x100,D0
  62.  
  63.         move.l    D0,_progsize    ;save program size
  64.         move.l    D0,-(A7)        ;amount of memory to keep
  65.         move.l    A5,-(A7)        ;starting at basepage
  66.         clr        -(A7)            ;junk word
  67.         move    #0x4a,-(A7)        ;Mshrink() - return excess storage
  68.         trap    #1
  69.         lea        12(A7),A7
  70.  
  71.     cont:
  72.         move.l    A5, _base;        ;so user progs can find basepage
  73.  
  74.         jsr        main            ;call applications entry point
  75.  
  76.         clr        -(A7)            ;do Pterm0() on return
  77.         trap    #1
  78. }
  79.